package com.ruoyi.system.schedule;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.constant.StatisticConst;
import com.ruoyi.system.domain.PaymentRecord;
import com.ruoyi.system.domain.StatisticWebsite;
import com.ruoyi.system.mapper.StatisticWebsiteMapper;
import com.ruoyi.system.service.IPaymentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@EnableScheduling
@Component
public class StatisticWebsiteSchedule {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private StatisticWebsiteMapper statisticWebsiteMapper;

    @Autowired
    private IPaymentRecordService paymentRecordService;

    @Scheduled(cron ="0 0 0 * * ?")
//    @PostConstruct
    public void syncData(){
        Date yesterdayDate = DateUtils.addDays(DateUtils.getNowDate(), -1);
        String yesterdayDate8 = DateUtils.dateTime8(yesterdayDate);

        String pv = stringRedisTemplate.opsForValue().get(String.format(StatisticConst.PV, yesterdayDate8));
        Long uv = stringRedisTemplate.opsForSet().size(String.format(StatisticConst.UV, yesterdayDate8));
        StatisticWebsite statisticWebsite = new StatisticWebsite();
        statisticWebsite.setFlowDate(Integer.parseInt(yesterdayDate8));
        statisticWebsite.setPv(0L);
        statisticWebsite.setUv(0L);
        if(Objects.nonNull(pv)){
            statisticWebsite.setPv(Long.parseLong(pv));
        }
        if(Objects.isNull(uv)){
            statisticWebsite.setUv(uv);
        }
        statisticWebsiteMapper.insert(statisticWebsite);

        stringRedisTemplate.delete(String.format(StatisticConst.PV, yesterdayDate8));
        stringRedisTemplate.delete(String.format(StatisticConst.UV, yesterdayDate8));
    }


    // 30分钟执行一次
    @Scheduled(cron ="0 */30 * * * ?")
    public void updateInterimPaymentRecords(){
        // 创建时间超过30分钟，且未支付的订单跟新为已过期
        List<PaymentRecord> paymentRecords = paymentRecordService.list(
                Wrappers.lambdaQuery(PaymentRecord.class)
                        .eq(PaymentRecord::getPayStatus, "1")
                        // 数据库时间需要先加8小时获取当前时间，然后查询出创建时间超过30分钟的单据
                        .apply(
                                "DATE_FORMAT(date_add(date_sub(NOW(), INTERVAL 8 HOUR), INTERVAL -30 MINUTE), '%Y-%m-%d %H:%i') <= DATE_FORMAT(create_time, '%Y-%m-%d %H:%i')"
                        )
        );

        if (CollectionUtil.isEmpty(paymentRecords)) {
            return;
        }
        List<Long> ids = paymentRecords.stream()
                .map(PaymentRecord::getId)
                .collect(Collectors.toList());

        paymentRecordService.update(
                Wrappers.lambdaUpdate(PaymentRecord.class)
                        .in(PaymentRecord::getId, ids)
                        .set(PaymentRecord::getPayStatus, "4")
                        .set(PaymentRecord::getMsg, "支付已过期")
        );

    }
}
